#include "su03t.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "../my_device/my_device.h"
#include "../gh_main.h"
#include "../oled/oled.h"
uint8_t su03t_read[20] = {0};
su03t_recv sure={0};


uint8_t su_recv[UART_DATA_RECV_SIZE] = {0};
uart_buffer_config_t su03t_buffer_config = {.rx_buffer = su_recv, .rx_buffer_size = UART_DATA_RECV_SIZE};

void su03t_write(unsigned char *recv,size_t len){
    uapi_uart_write(CONFIG_SU03T_UART_BUS, recv, len, 0);
}


// void oled_read_handler(const void *buffer, uint16_t length, bool error)
// {
//     unused(error);
//     if (buffer == NULL || length == 0) {
//         return;
//     }
//     if (memcpy_s(su03t_read, length, buffer, length) != EOK) {
//         return;
//     }

//     printf("");

// }
// 读取队列中接收到的回复 并打印
void recv_uart_hex(su03t_recv* recv)
{
    printf("recv:");
    for (uint16_t k = 0; k < recv->recv_len; k++) {
        /* code */
        printf("%X ", recv->recv[k]);
    }
    printf("\r\n");
    // 解析数据包
    voice_control_data(recv->recv);
}
/* 串口接收回调 */

void voice_control_data(uint8_t *pstr)
{

    // uint8_t cmd[6] = {0xAA, 0X55, 0X01, 0X00, 0x55, 0xAA};
    SensorData* data =get_Sen();
    switch (pstr[0]) {
        case 0x01: // 获取单个数据
            {
                switch (pstr[1])
                {
                case 0x01://hum4
                    su03t_send_doudata(data->humidity,0x04);
                    break;
                case 0x02://tem5
                    su03t_send_doudata(data->temperature,0x05);
                    break;
                case 0x03://l
                    su03t_send_intdata(data->ligBuffer,0x06);
                    break;
                case 0x04://s
                    su03t_send_intdata(data->soilHum,0x07);
                    break;
                case 0x05://uv
                    su03t_send_intdata(data->uv,0x08);
                    break;
                case 0x06://co
                    su03t_send_intdata(data->co2,0x09);
                    break;
                case 0x07://rain
                    su03t_send_intdata(data->rain,0x0A);
                    break;
                default:
                    break;
                }
            }
            break;
        case 0x02: // 控制
            {

            }
            break;
        case 0x03: //土壤湿度阈值 
            {

            }
        break;
        case 0x04: // 后退
            {

            }
            break;
        case 0x05: // id
            {
                if(pstr[1]==0x01){
                    now_id++;
                    set_oled_iddata(now_id);

                }else if(pstr[1]==0x02){
                    now_id--;
                    set_oled_iddata(now_id);

                }
            }
            break;
        default:
            break;
    }
}

void su03t_send_alldata() {
    SensorData* data =get_Sen();
    char frame[64] = {0}; // 帧缓冲区，足够大以存储所有数据
    char recv[64] = {0};  // 用于存储待发送的数据

    // 定义帧头和帧尾
    unsigned char frame_head[] = {0xAA, 0x55, 0x01};
    unsigned char frame_tail[] = {0x55, 0xAA};

    // 当前写入位置
    size_t offset = 0;

    // 将帧头添加到帧缓冲区
    memcpy(frame, frame_head, sizeof(frame_head));
    offset += sizeof(frame_head);

    // 将 h 转换为 double 并添加到帧缓冲区
    double humidity_val;
    sscanf(data->humidity, "%lf", &humidity_val);
    memcpy(frame + offset ,&humidity_val, sizeof(double));
    offset += sizeof(double);

    // 将 t 转换为 double 并添加到帧缓冲区
    double temperature_val;
    sscanf(data->temperature, "%lf", &temperature_val);
    memcpy(frame + offset , &temperature_val, sizeof(double));
    offset += sizeof(double);

    // 将 l 转换为 int 并添加到帧缓冲区
    int lig_val;
    sscanf(data->ligBuffer, "%d", &lig_val);
    memcpy(frame + offset, &lig_val, sizeof(int));
    offset += sizeof(int);

    // 将 s 转换为 int 并添加到帧缓冲区
    int soil_hum_val;
    sscanf(data->soilHum, "%d", &soil_hum_val);
    memcpy(frame + offset , &soil_hum_val, sizeof(int));
    offset += sizeof(int);

    // 将 u 转换为 int 并添加到帧缓冲区
    int uv_val;
    sscanf(data->uv, "%d", &uv_val);
    memcpy(frame + offset , &uv_val, sizeof(int));
    offset += sizeof(int);

    // 将 s 转换为 int 并添加到帧缓冲区
    int co_val;
    sscanf(data->co2, "%d", &co_val);
    memcpy(frame + offset , &co_val, sizeof(int));
    offset += sizeof(int);
    // 将 s 转换为 int 并添加到帧缓冲区
    int rain_val;
    sscanf(data->rain, "%d", &rain_val);
    memcpy(frame + offset , &rain_val, sizeof(int));
    offset += sizeof(int);

    // 将帧尾添加到帧缓冲区
    memcpy(frame + offset , frame_tail, sizeof(frame_tail));
    offset += sizeof(frame_tail);

    // 计算帧的总长度并发送
    size_t frame_length = offset;
    memcpy(recv, frame, frame_length); // 将帧数据复制到 recv 缓冲区

    // printf("%s\r\n",recv);
    uapi_uart_write(CONFIG_SU03T_UART_BUS, (unsigned char *)recv, frame_length, 0); // 发送帧数据
    // su03t_write((unsigned char *)recv,frame_length);

    printf("Frame sent successfully!\n");
}
void su03t_send_intdata(char* l,uint8_t id) {
    char frame[50] = {0}; // 帧缓冲区，足够大以存储所有数据
    char recv[50] = {0};  // 用于存储待发送的数据

    // 定义帧头和帧尾
    unsigned char frame_head[] = {0xAA, 0x55, id};
    unsigned char frame_tail[] = {0x55, 0xAA};

    // 将帧头添加到帧缓冲区
    memcpy(frame, frame_head, sizeof(frame_head));

    // 将 l 转换为 int 并添加到帧缓冲区
    int lig_val;
    sscanf(l, "%d", &lig_val);
    memcpy(frame + sizeof(frame_head), &lig_val, sizeof(lig_val));
    // 将帧尾添加到帧缓冲区
    memcpy(frame + sizeof(frame_head) + sizeof(lig_val), frame_tail, sizeof(frame_tail));

    // 计算帧的总长度并发送
    size_t frame_length = sizeof(frame_head) + sizeof(lig_val) + sizeof(frame_tail);
    memcpy(recv, frame, frame_length); // 将帧数据复制到 recv 缓冲区
    // uapi_uart_write(CONFIG_UART_BUS, recv, frame_length, 0); // 发送帧数据
    su03t_write((unsigned char *)recv,frame_length);

    printf("Frame sent successfully!\n");
}

void su03t_send_doudata(char* l,uint8_t id){
    char frame[50] = {0}; // 帧缓冲区，足够大以存储所有数据
    char recv[50] = {0};  // 用于存储待发送的数据

    // 定义帧头和帧尾
    unsigned char frame_head[] = {0xAA, 0x55, id};
    unsigned char frame_tail[] = {0x55, 0xAA};

    // 将帧头添加到帧缓冲区
    memcpy(frame, frame_head, sizeof(frame_head));

    // 将 l 转换为 dou 并添加到帧缓冲区
    double tmp_val;
    sscanf(l, "%lf", &tmp_val);
    memcpy(frame + sizeof(frame_head), &tmp_val, sizeof(tmp_val));
    // 将帧尾添加到帧缓冲区
    memcpy(frame + sizeof(frame_head) + sizeof(tmp_val), frame_tail, sizeof(frame_tail));

    // 计算帧的总长度并发送
    size_t frame_length = sizeof(frame_head) + sizeof(tmp_val) + sizeof(frame_tail);
    memcpy(recv, frame, frame_length); // 将帧数据复制到 recv 缓冲区
    // uapi_uart_write(CONFIG_UART_BUS, recv, frame_length, 0); // 发送帧数据
    su03t_write((unsigned char *)recv,frame_length);

    printf("Frame sent successfully!\n");
}

void su03t_read_handler(const void *buffer, uint16_t length, bool error)
{
    unused(error);
    if (buffer == NULL || length == 0) {
        return;
    }
    sure.recv_len = 0;
    memset(sure.recv, 0, sizeof(sure.recv));
    memcpy_s(sure.recv, length, buffer, length);
    sure.recv_len = length;
    recv_uart_hex(&sure);
}

void su_init(){
    uart_gpio_init(CONFIG_SU03T_UART_BUS,CONFIG_SU03T_TXD_PIN,CONFIG_SU03T_RXD_PIN);
    uart_init_config(CONFIG_SU03T_UART_BUS,CONFIG_SU03T_TXD_PIN,CONFIG_SU03T_RXD_PIN,&su03t_buffer_config);
    uapi_uart_register_rx_callback(CONFIG_SU03T_UART_BUS, UART_RX_CONDITION_MASK_IDLE, 10, su03t_read_handler);

}